﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.IO;

using NUnit.Framework;

using Refix.Core.Commands;

namespace Refix.Core.Tests.Commands
{
    [TestFixture]
    public class SingleSolutionCommandTest : TestSolutionFolderTest
    {
        [Test]
        public void FileNameSetter_Roots_Value()
        {
            var clean = new Clean();

            clean.FileName = "test.txt";
            Assert.IsTrue(Path.IsPathRooted(clean.FileName));
        }

        [Test]
        public void Validate_Calls_Base_Validate_Method()
        {
            var clean = new CommandWithOptions(new[] {"clean", "-qv"});
            Assert.False(clean.Command.Validate(),
                         "Base Validate method should have failed for conflicting options.");
        }

        [Test]
        public void Validate_Fails_With_Too_Many_Arguments()
        {
            var clean = new CommandWithOptions(new[] { "clean", "1.sln", "2.sln" });
            Assert.False(clean.Command.Validate(),
                         "Validate method should have failed for multiple filenames as arguments.");
        }

        [Test]
        public void Validate_Fails_With_Too_Many_Solutions_In_Working_Folder()
        {
            var clean = new CommandWithOptions(new[] { "clean" });
            Assert.False(clean.Command.Validate(),
                         "Validate method should have failed for multiple files present.");
        }

        [Test]
        public void Validate_Fails_For_Non_Existent_File()
        {
            var clean = new CommandWithOptions(new[] { "clean", "1.sln" });
            Assert.False(clean.Command.Validate(),
                         "Validate method should have failed for non-existent solution file.");
        }

        [Test]
        public void Validate_Passes_For_Valid_File()
        {
            var clean = new CommandWithOptions(new[] { "clean", "Refix.TestSolution.sln" });
            Assert.True(clean.Command.Validate(),
                        "Validate method should have passed for valid solution file.");
        }

        [Test]
        public void Validate_Fails_For_No_Solution_Files()
        {
            string folder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
            Directory.CreateDirectory(folder);
            string oldWorkingFolder = Directory.GetCurrentDirectory();
            Directory.SetCurrentDirectory(folder);
            var clean = new CommandWithOptions(new[] { "clean" });
            Assert.False(clean.Command.Validate(),
                         "Validate method should have failed for no solution files.");
            Directory.SetCurrentDirectory(oldWorkingFolder);
            Directory.Delete(folder, true);
        }

        [Test]
        public void Validate_Passes_For_Single_Solution_File()
        {
            string folder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
            Directory.CreateDirectory(folder);
            string oldWorkingFolder = Directory.GetCurrentDirectory();
            Directory.SetCurrentDirectory(folder);
            File.WriteAllText(Path.Combine(folder, "1.sln"), "x");
            var clean = new CommandWithOptions(new[] { "clean" });
            Assert.True(clean.Command.Validate(),
                        "Validate method should have passed for single solution files.");
            Directory.SetCurrentDirectory(oldWorkingFolder);
            Directory.Delete(folder, true);
        }
    }
}